package com.village.zpark.controller;

import com.village.zpark.common.ResultMapUtil;
import com.village.zpark.pojo.Admin;
import com.village.zpark.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;


    @PostMapping("/login")
    public HashMap<String, Object> loginController(@RequestBody Admin admin,HttpSession session) {

        Admin login = adminService.login(admin);
        if (login == null) {
            return ResultMapUtil.getHashMapLogin("登录失败", "500","");
        }
        if (login.getRole().equals("1")) {
            session.setAttribute("loginUser", login);
            return ResultMapUtil.getHashMapLogin("登录成功", "200","0");
        }else {
            session.setAttribute("loginUser", login);
            return ResultMapUtil.getHashMapLogin("登录成功", "200","1");
        }

    }

//    @PostMapping("/add")
//    public HashMap<String, Object> logoutController(@RequestBody Admin admin) {
//        if (adminService.add(admin) != 0) {
//            return ResultMapUtil.getHashMapLogin("注册成功", "200","1");
//        } else {
//            return ResultMapUtil.getHashMapLogin("注册失败", "500","1");
//
//        }
//    }

    @RequestMapping(value = "/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "/login.html";
    }

    @RequestMapping(value = "/register")
    public String register() {
        return "/register";
    }
    @PostMapping("/registerUser")
    @ResponseBody
    public Map<String, Object> register(@RequestParam("adminname") String adminname,
                                        @RequestParam("password") String password,
                                        @RequestParam("confirm_password") String confirmPassword,
                                        @RequestParam("role") String role) {
        Map<String, Object> result = new HashMap<>();

        // 检查两次密码是否一致
        if (!password.equals(confirmPassword)) {
            result.put("code", "0");
            result.put("msg", "两次密码输入不一致");
            return result;
        }

        // 创建新用户
        Admin newAdmin = new Admin();
        newAdmin.setAdminname(adminname);
        newAdmin.setPassword(password);
        newAdmin.setRole(role);

        // 保存用户
        boolean add = adminService.add(newAdmin);
        if (add) {
            result.put("code", "1");
            result.put("msg", "注册成功");
        } else {
            result.put("code", "0");
            result.put("msg", "注册失败，请重试");
        }
        return result;
    }
//    public Map<String, Object> register(@RequestParam("adminname") String adminname,
//                                        @RequestParam("password") String password,
//                                        @RequestParam("confirm_password") String confirmPassword,
//                                        @RequestParam("role") String role){
//        System.out.println("注册信息"+adminname + password + confirmPassword + role);
//        Map<String, Object> result = new HashMap<>();
//        // 检查两次密码是否一致
//        if (!password.equals(confirmPassword)) {
//            result.put("code", "0");
//            result.put("msg", "两次密码输入不一致");
//            result.put("icon", 2);
//            result.put("anim", 6);
//            return result;
//        }
//
//        // 创建新用户
//        Admin newAdmin = new Admin();
//        newAdmin.setAdminname(adminname);
//        newAdmin.setPassword(password);
//        newAdmin.setRole(role);
//
//        // 保存用户
//        boolean add = adminService.add(newAdmin);
//        if (add) {
//            result.put("code", "1");
//            result.put("msg", "注册成功");
//            result.put("icon", 1);
//            result.put("anim", 1);
//        } else {
//            result.put("code", "0");
//            result.put("msg", "注册失败，请重试");
//            result.put("icon", 2);
//            result.put("anim", 6);
//        }
//        return result;
//    }

}


